package com.egg.ylt.presenter.impl;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.pojo.RelationEntity;
import com.egg.ylt.pojo.SwitchBabyEntity;
import com.egg.ylt.pojo.UpLoadFileEntity;
import com.egg.ylt.presenter.IEditBabyPresenter;
import com.egg.ylt.view.IEditBabyView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.io.File;
import java.util.HashMap;

/**
 * Created by sichaoqi on 2018/10/10.
 */

public class EditBabyPresenterImpl extends BasePresenter<IEditBabyView> implements IEditBabyPresenter {

    @Override
    public void postBabyInfo(String token, String name, String birthday, String height, String weight,
                             String imageUrl, String id, String gender, String relationshipId, String relationshipName) {

        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("name", name);
        hashMap.put("birthday", birthday);
        hashMap.put("height", height);
        hashMap.put("weight", weight);
        hashMap.put("imageUrl", imageUrl);
        hashMap.put("id", id);
        hashMap.put("gender", gender);
        hashMap.put("relationshipId", relationshipId);
        hashMap.put("relationshipName", relationshipName);
        RequestManager.getInstance().requestPostByAsyn(API.GET_ADD_MEMBER_BABY, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.addBabySucceed();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
//                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }
    @Override
    public void upImg(String file) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, Object> hashMap = new HashMap<>();
        File photoFile = new File(file);
        hashMap.put("file", photoFile);
        RequestManager.getInstance().upLoadFile(API.UP_LOAD_FILE, hashMap, new ReqCallBack<UpLoadFileEntity>() {

            @Override
            public void onReqSuccess(UpLoadFileEntity result) {
                mView.dismissDialogLoading();
                mView.getImgUri(result);
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    public void getRelationList() {
        mView.showDialogLoading("正在加载中...");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        RequestManager.getInstance().requestGetByAsyn(API.GET_RELATION_LIST, hashMap, new ReqCallBack<RelationEntity>() {
            @Override
            public void onReqSuccess(RelationEntity result) {
                mView.dismissDialogLoading();
                mView.updateRelationList(result.getList());
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showError(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showError(error.getMsg());
            }
        });
    }

}
